package com.travel.mapper.user;

import com.travel.model.entity.auth.User;
import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
//用户mapper 查询用户 插入用户 更新用户 
@Mapper
public interface UserMapper {
    
    @Select("SELECT * FROM users WHERE id = #{id}")
    Optional<User> findById(@Param("id") Long id);
    
    @Select("SELECT * FROM users WHERE username = #{username}")
    Optional<User> findByUsername(@Param("username") String username);
    
    @Select("SELECT COUNT(*) > 0 FROM users WHERE username = #{username}")
    boolean existsByUsername(@Param("username") String username);
    
    @Select("SELECT COUNT(*) > 0 FROM users WHERE email = #{email}")
    boolean existsByEmail(@Param("email") String email);
    
    @Insert("INSERT INTO users (username, password, email, phone, nickname, avatar, status, created_time) " +
            "VALUES (#{username}, #{password}, #{email}, #{phone}, #{nickname}, #{avatar}, #{status}, #{createdTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);
    
    @Select("SELECT * FROM users LIMIT #{pageable.offset}, #{pageable.pageSize}")
    @Results({
        @Result(property = "id", column = "id"),
        // ... 其他字段映射
    })
    List<User> findAll(@Param("pageable") Pageable pageable);
    
    @Select("SELECT COUNT(*) FROM users")
    long count();
    
    @Update("UPDATE users SET " +
            "nickname = #{nickname}, " +
            "avatar = #{avatar}, " +
            "phone = #{phone}, " +
            "email = #{email}, " +
            "password = #{password}, " +
            "status = #{status}, " +
            "updated_time = #{updatedTime} " +
            "WHERE id = #{id}")
    int update(User user);
} 